package app.modules.cloudpivot.sso;

import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;

import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.web.sso.security.BaseAuthenticationToken;
import com.authine.cloudpivot.web.sso.template.BaseSimpleTemplate;
import com.authine.cloudpivot.web.sso.template.ResponseTypeEnum;
import com.authine.cloudpivot.web.sso.template.SimpleTemplateConfig;

@Component
public class RedirectAuth extends BaseSimpleTemplate {

    protected RedirectAuth() {
        super(SimpleTemplateConfig.builder()
                .requestMatcher(new AntPathRequestMatcher("/login/redirect", HttpMethod.GET.name()))
                .responseType(ResponseTypeEnum.REDIRECT)
                .redirectClientID("api")
                .loginPassredirectUrl("https://cloudpivot.lfans.cn:28443/api/oauth/authorize?client_id=api&response_type=code&scope=read&redirect_uri=https://cloudpivot.lfans.cn:28443/oauth")
                .loginFailRedirectUri("https://cloudpivot.lfans.cn:28443/login")
                .build());
    }

    @Override
    public String getSourceId(BaseAuthenticationToken token) throws Exception {
        return token.getCode();
    }

    @Override
    public UserModel getUser(Object sourceId) {
        return engineService.getOrganizationFacade().getUserByMobile(sourceId.toString());
    }
}
